package nickthissen.iracing.parsing;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import nickthissen.iracing.datamodels.Result;

/**
 * Created by Nick on 3-6-13.
 */
public class MessageParser extends Parser
{
    @Override
    protected Result doParse(String source)
    {
        Result result = new Result();

        log("Start jsoup parsing message.");
        Document doc = Jsoup.parse(source);
        log("Finished jsoup parsing message.");

        Elements textNodes = doc.select("textarea[class=message]");
        if (textNodes.size() != 1)
        {
            log("Incorrect number of text nodes found. Expected: 1, found: " + textNodes.size());
        }

        Element textNode = textNodes.first();

        result.Object = HttpUtil.htmlDecode(textNode.text());
        result.Success = true;
        return result;
    }

    @Override
    protected String getLogName()
    {
        return "MessageParser";
    }

    @Override
    protected Result createDefaultResultObject()
    {
        Result result = new Result();
        result.Object = "";
        result.Message = "Error parsing message.";
        return result;
    }
}
